/*!
 * 一些工具
 */
if(!commonUtil){
	var commonUtil = {};
}

$.extend(commonUtil,{
	//一些常量
	msgId:"#msgDialog"
	,confirmId:"#confirmDialog"
		
	//用map对象的值替换字符串里key名相同的值
	,mapReplaceStr:function(str,map,index){
		if(!map){
			return '';
		}
		if(index!=null && index!=undefined){
			str = str.replace(/\{INDEX\}/g,index);
		}
		for(var key in map){
			var obj = map[key];
			if(obj==undefined||obj===''){
				str = str.replace("{"+key+"}","&nbsp;");
			}else if(typeof(obj)=="object"){
				for(var keySub in obj){
					str = str.replace("{"+key+"."+keySub+"}",(obj[keySub]==undefined||obj[keySub]==='')?"&nbsp;":obj[keySub]);
				}
			}else{
				str = str.replace("{"+key+"}",(map[key]==undefined||map[key]==='')?"&nbsp;":map[key]);
			}
		}
		return str;
	},
	//按字符串规则或回调函数规则生成tbody内容
	generateDataTable:function(list,containerId,tempOrFunc){
		var container = $("#"+containerId);
		if(container.length==0){
			return;
		}
		container.empty();
		$.each(list,function(i,data){
			var rowHtml = "";
			if($.isFunction(tempOrFunc)){//每行一个大函数
				rowHtml = tempOrFunc(data,i,container);
			}else if($.isArray(tempOrFunc)){//一个数组，每小格对应一个模版文字或函数
				var tr = document.createElement("TR");
				$.each(tempOrFunc,function(subi,subn){
					if($.isFunction(subn)){
						$(tr).append("<td>"+subn(n,i,subi,tr)+"</td>");
					}else{
						var tdHtml = commonUtil.mapReplaceStr(subn,data,i);;
						$(tr).append("<td>"+tdHtml+"</td>");
					}
				});
				container.append(tr);
			}else{//每行一个大模版文字
				rowHtml = commonUtil.mapReplaceStr(tempOrFunc,data,i);
			}
			container.append(rowHtml);
		});
//		$("#"+containerId +" tr:nth-child(even)").children().addClass("even");
//		$("#"+containerId +" tr:nth-child(odd)").children().addClass("odd");
	}
	//异步post发送请求，返回值是json对象
	,jsonPost:function(url,params,callbackFunc,loadingObjs){
		this.showLoading(loadingObjs);
		$.ajax({
			type: "POST",
			url: url,
			async:true,
			data:params,
			dataType: "json",
			success: function(data){
				if (data.msg == null || data.msg == '') {
					if (callbackFunc) {
						callbackFunc(data);
					}
				} else {
					alert(data.msg);
				}
			},
			error:function(e){
				if(e.status==200){
					alert(e.responseText);
				}else{
					alert("出错了，\n\n可能是服务器重启或者后台报错，刷新要是不管用的话去看日志吧。");
				}
			}
		});
	}
	//异步post发送请求，返回值是html页面
	,htmlPost:function(url,params,targetId,callbackFunc){
		this.showLoading('#'+targetId);
		$('#'+targetId).load(url,params,function(data){
			if(data.msg==null || data.msg==''){
				if(callbackFunc){
					callbackFunc(data);
				}
				$('#'+targetId).css("display","");
			}else{
				alert(data.msg);
			}
		});
	}
	//获取url中的参数，注意此方法没有处理同名参数，xxx.do?a=5&a=6将只能得到6
	,getUrlParam:function(key){ 
		var url=location.href; 
		var params = {};
		var questPost = url.indexOf("?");
		var urlLength = url.length;
		if( questPost!=-1 && questPost!=(urlLength-1) ){
			var urlParams = url.substring( questPost+1,urlLength );
			var paramArray = urlParams.split("&");
			if(paramArray.length!=0){
				for(i=0;i<paramArray.length;i++){
					var keyValue = paramArray[i];
					var equalPost = keyValue.indexOf("=");
					if( equalPost==-1 ){
						continue;
					}
					params[keyValue.substring(0,equalPost)] = keyValue.substring(equalPost+1,keyValue.length);
				}
			}
		}
		if(key){
			return (params[key]==undefined||params[key]==null)?"":params[key];
		}
		return params;
	}
	//去掉字符串前后的空格
	,trimStr:function(str) {
		   return str.replace(/^\s+|\s+$/g,"");
	}
	//判断是否空白字符串
	,isBlank:function(param){
		if(param==undefined||param==null||param=="")
			return true;
		else if(param.replace(/^\s+|\s+$/g,"")=="")
			return true;
		else 
			return false;
	}
	//显示装载中的图片
	,showLoading:function(obj){
		try{
			if($(obj).length>0){
				var img = "<span><img src='../images/loading.gif' /></span>";
				if($(obj).get(0).tagName=="TBODY"){
					img = "<tr><td colspan='10'><img src='../images/loading.gif' /></td></tr>";
				}
				$(obj).html(img);
			}
		}catch(e){}
	}
	//让这个文本框显示支持模糊查询
	,likeTextLabel:"支持模糊查询"
	,likeText:function(obj){
		var likeMsg = this.likeTextLabel;
		var obj = $(obj);
		obj.blur(function(){
			var value = this.value;
			if(value==""){
				this.value = likeMsg;
				$(this).css("color","#aaaaaa");
			}
		});
		obj.focus(function(){
			$(this).css("color","#000000");
			var value = this.value;
			if(value==likeMsg){
				this.value = "";
			}
		});
		obj.blur();
	}
	,getLikeTextVal:function(id){
		return $(id).val()==commonUtil.likeTextLabel?"":$(id).val()
	}
	//获得某容器下所有输入框的值map，非disabled的文本
	,getInputValues:function(obj){
		var result = {};
		var elements = $(obj+' input[type!=radio][type!=checkbox]:enabled,'+obj+' input:checked,'+obj+' select:enabled,'+obj+' textarea:enabled,'+obj+' :hidden');
		$.each(elements,function(i,n){
			if(n.value && n.name){
				if(result[n.name]){
					result[n.name] += "," + n.value;
				}else{
					result[n.name] = n.value;
				}
			}
		});
		return result;
	}
	//给容器下元素赋值
	,setInputValues:function(obj,map,preFix){
		if(!preFix) preFix="";
		$.each(map,function(i,n){
			var name = preFix + i;
			$(obj+' input[type!=radio][type!=checkbox][name="'+name+'"],'+obj+' textarea[name="'+name+'"],'+obj+' :hidden[name="'+name+'"],'+obj+' select[name="'+name+'"]').val(n);
			$(obj+' input[type=radio][name="'+name+'"],'+obj+' input[type=checkbox][name="'+name+'"]').attr("checked",false);
			$(obj+' input[type=radio][name="'+name+'"][value='+n+']').attr("checked",true);
			if($(obj+' input[type=checkbox][name="'+name+'"]').length>0){
				$(obj+' input[type=checkbox][name="'+name+'"]').attr("checked",false);
				var values = n.split(",");
				$.each(values,function(subi,subn){
					$(obj+' input[type=checkbox][name="'+name+'"][value='+subn+']').attr("checked",true);
				});
			}
		});
	}
	//配合页面msgDialog，显示alert信息框
	,showAlert:function(title,msg,func){
		$(commonUtil.msgId).dialog({
			show: "fade",
			modal: true,
			title:title,
			buttons: {
				"关闭":function(){
					if (func) {
						func();
					}
					$(this).dialog( "close" );
				}
			}
		}).find("span").eq(1).html(msg);
	}
	//配合页面msgDialog，显示确认信息框
	,showConfirm:function(title,msg,buttons){
		if(!buttons){
			buttons = {};
		}
		$.extend(buttons,{
			"取消":function(){ $(this).dialog("close"); } 
		});
		$(commonUtil.confirmId).dialog({
			show: "fade",
			modal: true,
			title:title,
			buttons: buttons
		}).find("span").eq(1).html(msg);
	}
});

